var out_video = document.getElementById('indexJs').getAttribute('out_video');
var pic_type = document.getElementById('indexJs').getAttribute('pic_type');
var page = 1;
var category_id = 0;
var buyed = 0;
var random_view = 1;
var searchkey = '';
var recommend = 0;
var selltop = 0;
errImgs = new Array();
var code = getQueryVariable("code") ;
function loadRandomCurrent(){
    random_view = 1;
    selltop = 0;
    refresh();
}
function loadRecommand(){
    hui.scrollTop(0)
    searchkey='';
    $('#searchKey').val('');
    random_view = 1;
    buyed = 0;
    category_id = 0;
    selltop = 0;
    recommend = 1;
    document.title="热门推荐";
    refresh();
}
function loadRandom(){
    hui.scrollTop(0)
    searchkey='';
    $('#searchKey').val('');
    random_view = 1;
    buyed = 0;
    category_id = 0;
    recommend = 0;
    selltop = 0;
    document.title="随便查看";
    refresh();
}
function loadAll(){
    hui.scrollTop(0)
    searchkey='';
    $('#searchKey').val('');
    random_view = 0;
    buyed = 0;
    category_id = 0;
    recommend = 0;
    selltop = 0;
    document.title="最新";
    refresh();
}
function loadBuy(){
    hui.scrollTop(0)
    searchkey='';
    $('#searchKey').val('');
    random_view = 1;
    buyed = 1;
    category_id = 0;
    recommend = 0;
    selltop = 0;
    document.title="已购";
    refresh();
}
function sellTop(){
    hui.scrollTop(0)
    $('#searchKey').val('');
    searchkey='';
    random_view = 1;
    buyed = 0;
    recommend = 0;
    category_id = 0;
    selltop = 1;
    document.title='网友最爱';
    refresh();
}
function loadCategory(e){
    hui.scrollTop(0)
    $('#searchKey').val('');
    searchkey='';
    random_view = 1;
    buyed = 0;
    recommend = 0;
    selltop = 0;
    category_id = e.getAttribute('data-id');
    document.title=e.getAttribute('data-title');
    refresh();
}
document.getElementById('searchKey').addEventListener('keyup', function(e){if(e.keyCode == 13){search();}});
function search(){
    hui.scrollTop(0)
    searchkey=$('#searchKey').val();
    if(searchkey==''){
        hui.iconToast('请输入搜索关键词', 'warn');
        return false;
    }
    random_view = 1;
    buyed = 0;
    category_id = 0;
    recommend=0;
    selltop = 0;
    document.title="搜索";
    refresh();
}
function getMore(){
    $.ajax({
        type : 'POST',
        url : '/api/videos',
        data : {
            code:code,
            page:page,
            category_id:category_id,
            buyed:buyed,
            random_view:random_view,
            search:searchkey,
            recommend:recommend,
            distinct:1,
            pic_type:pic_type,
            selltop :selltop
        },
        timeout:3500,
        backType:'JSON',
        beforeSend : function(){},
        complete   : function(XMLHttpRequest,status){
            if(status=='timeout'){//超时,status还有success,error等值的情况
     　　　     hui.upToast('网络异常，上下划动屏幕重试！');
     　　　     hui.resetLoadMore();
    　　　　}
        },
        success : function(res){
            if(res['status'] == 'success'){
                appendData(res['data']['videos'],pic_type,code,res['data']['img_cdns']||[],res['data']['img_domain']||'');
                page ++;
                if(res['data']['videos'].length <=0){
                    if(buyed){
                        hui.endLoadMore(true,'没有了');
                        return false;
                    }else if(searchkey !=''){
                        hui.endLoadMore(true,'没有了');
                        return false;
                    }else{
                        page=1
                    }
                }
                hui.endLoadMore();
            }else{
                hui.resetLoadMore();
                hui.iconToast('请求错误，上下划动屏幕重试', 'warn');
            }
        },
        error: function (jqXHR, textStatus, errorThrown) {
            hui.resetLoadMore();
            hui.upToast('网络异常，上下划动屏幕重试！');
        }
    })
}
function refresh(){
    page = 1;
    hui.ajax({
        type : 'POST',
        url : '/api/videos',
        data : {
            code:code,
            page:1,
            category_id:category_id,
            buyed:buyed,
            random_view:random_view,
            search:searchkey,
            recommend:recommend,
            selltop :selltop,
            pic_type:pic_type            
        },
        timeout:10000,
        backType:'JSON',
        beforeSend : function(){ 
            hui.loading('加载中...');
        },
        complete   : function(){
            hui.closeLoading();
            hui.endRefresh();
        },
        success : function(res){
            if(res['status'] == 'success'){
                document.getElementById('video_list').innerHTML = "";
                appendData(res['data']['videos'],pic_type,code,res['data']['img_cdns']||[],res['data']['img_domain']||'');
                page =2;
                if(res['data']['videos'].length <=0){
                    hui.endLoadMore(true,'更多视频24小时不间断上传中...');
                    return false;
                }
                hui.resetLoadMore();
            }else{
                hui.iconToast('请求错误，请重试', 'warn');
            }
        },
        error : function(e){
            hui.upToast('网络异常，请重试！');
        }
    })
}
function reportUrlErr(path,type){
    if(errImgs.length > 0){
        hui.postJSON('/api/url-error',
            {
                code:code,
                path:path,
                type:type
            },
            function(res){
            }
        )
    }
}
window.onscroll = function() {
    if (document.body.scrollTop > 30 || document.documentElement.scrollTop > 30) {
        document.getElementById("toTop").style.display = "flex";
    } else {
        document.getElementById("toTop").style.display = "none";
    }
}
function backTop(){
    document.body.scrollTop = 0;
    document.documentElement.scrollTop = 0;
}
function tousu(){
    location.href='/tousu/index.html?code='+code;
}
$(".video-item").click(function(){
    let url = $(this).attr('data-url');
    let face = $(this).find('img').attr("src");
    if(buyed){
        window.location.href=url;
    }else{
        let title = $(this).attr('data-title');
        hui('#title').html(title);
        hui('.todetail').attr('href',url);
        hui('.now-pic').attr('src',face) ;
        hui.blackMask();
        hui.shwoBlackMasker();
        if(videodomain && previewvideo){
            let tips = "↓↓8秒试看（总时长"+ $(this).attr('data-time')+")↓↓"
            hui('#view-tips').html(tips)
            let videourl = $(this).attr("data-videourl");
            videoPreplayer.newVideo({autoplay:false,video:"http://"+videodomain+videourl+"?start=300%26end=308",'poster':face});
        }else{
            let tips = "（总时长"+ $(this).attr('data-time')+")"
            hui('#view-tips').html(tips)
        }
    }
})
if(out_video){
    $('#show-out-video').show();
    var videoObject = {
        container: '.video-player',
        variable: 'player',
        video: out_video,
        mobileAutoFull: false,
        poster: './images/mobile/face.jpg',
    };
    var player = new ckplayer(videoObject);
}
window.onload=function(){
    $('#toTop').hide();
    hui.loadMore(getMore,'上拉加载更多');
    hui.refresh('#refreshContainer', refresh);
    hui.scrollNews(news,5000);
}
